<?php
class GiftCodeService {
    /**
     * @var GmService
     */
    private static $instance;

    /**
     * Get the instance of GiftCodeService
     *
     * @return GiftCodeService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new GiftCodeService();
        }
        return self::$instance;
    }
    /**
     * Construction.
     */
    private function __construct() {

    }

    /**
     * @param $userId
     * @param $codeId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function reward($userId,$codeId,$reqTime) {
        $res = array(
            'code' => 0,
            'reward' => array()
        );


       /* if ($codeId != 666666 && $codeId != 888888){
            $res['code'] = 29017;
            return $res;
        }*/

        $giftCodeSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GIFT_CODE, $codeId, true);
        if (!$giftCodeSetting) {
            $res['code'] = 29017;
            return $res;
        }
        $expire = strtotime($giftCodeSetting['validity']);
        if ($expire < $reqTime) {
            $res['code'] = 29018;
            return $res;
        }
        $userLevel = UserService::get()->getUserLevel($userId);
        if ($userLevel < $giftCodeSetting['limitLv']) {
            $res['code'] = 29021;
            return $res;
        }

        if (!$giftCodeSetting['gift']) {
            $res['code'] = 29019;
            return $res;
        }
        if ($giftCodeSetting['groupId'] == 1) {
            // 单个使用
            $giftCodeOne= SzPersister::get()->getVo(null, GameConst::ORM_NAME_GIFT_CODE_ONE, $codeId);
            if ($giftCodeOne) {
                $res['code'] = 29020;
                return $res;
            }
            $giftCodeOne = new GiftCodeOneVo($codeId,$userId,$reqTime,true);
            SzPersister::get()->setVo($giftCodeOne);

        } else {
            // 每个人使用
            $giftCodeMoreList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GIFT_CODE_MORE);
            $giftCodeMore = $giftCodeMoreList->getElement($codeId, false);
            if ($giftCodeMore) {
                $res['code'] = 29020;
                return $res;
            }

            $giftCodeMore = new GiftCodeMoreVo($userId,$codeId,$reqTime,true);
            $giftCodeMoreList->addElement($giftCodeMore);
            $giftCodeMoreList->setElement($codeId,$giftCodeMore);
            SzPersister::get()->setVoList($giftCodeMoreList);
        }

        $output = '';
        foreach ($giftCodeSetting['gift'] as $entityId=>$count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $output .= $entityId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(99999989,$userId,1,$output,'reward');//获得资源上报

        $res['reward'] = $giftCodeSetting['gift'];
        return $res;
    }
}